#include "TDALexicos.h"


int fCmpItemLexico(void *pLexico1,void *pLexico2)
{
	ITEM_LEXICO *lexico1 = (ITEM_LEXICO*)pLexico1;
	ITEM_LEXICO *lexico2 = (ITEM_LEXICO*)pLexico2;

	return strcmp(lexico1->lexico,lexico2->lexico);
}

int fOrderItemLexico(void *pLexico1,void *pLexico2)
{
	ITEM_LEXICO *lexico1 = (ITEM_LEXICO*)pLexico1;
	ITEM_LEXICO *lexico2 = (ITEM_LEXICO*)pLexico2;

	return strcmp(lexico1->lexico,lexico2->lexico);
}

int fCopyItemLexico(void *pTo,void *pFrom)
{
	ITEM_LEXICO *to = (ITEM_LEXICO*)pTo;
	ITEM_LEXICO *from = (ITEM_LEXICO*)pFrom;

	to->cantidadConsultasLexico = from->cantidadConsultasLexico;
	strcpy(to->lexico,from->lexico);

	return EXIT_SUCCESS;
}

int fCreateItemLexico(void *pItem)
{
	return EXIT_SUCCESS;
}

int fDestroyItemLexico(void *pItem)
{
	ITEM_LEXICO *item = (ITEM_LEXICO*)pItem;
	item->cantidadConsultasLexico = 0;
	strcpy(item->lexico,"");
	return EXIT_SUCCESS;
}


int tda_lexico_crear(TDA_LEXICOS *tdaLexico) {
	tdaLexico->listaLexicos = malloc(sizeof(TListaSimpleOrdenada));
	LSO_Crear(tdaLexico->listaLexicos, sizeof(ITEM_LEXICO),fCreateItemLexico,fCmpItemLexico,fCopyItemLexico,fDestroyItemLexico,fOrderItemLexico);
}

int tda_lexico_destruir(TDA_LEXICOS *tdaLexico) {
	LSO_Vaciar(tdaLexico->listaLexicos);
	free(tdaLexico->listaLexicos);
}

int tda_lexico_alta(TDA_LEXICOS *tdaLexico, ITEM_LEXICO *lexico) {
	return LSO_AgregarElemento(tdaLexico->listaLexicos,lexico);
}

int tda_lexico_incrementarConsultas(TDA_LEXICOS *tdaLexico,char *lexico)
{
	ITEM_LEXICO *itemLexico;
	itemLexico = malloc(sizeof(ITEM_LEXICO));
	fCreateItemLexico(itemLexico);

	strcpy(itemLexico->lexico,lexico);
	if (tda_lexico_consultar(tdaLexico, itemLexico))
	{
		itemLexico->cantidadConsultasLexico = itemLexico->cantidadConsultasLexico + 1;
		tda_lexico_modificar(tdaLexico, itemLexico);
	}

	fDestroyItemLexico(itemLexico);
	free(itemLexico);
	return EXIT_SUCCESS;
}


int tda_lexico_baja(TDA_LEXICOS *tdaLexico,ITEM_LEXICO *lexico){

	return LSO_EliminarElemento(tdaLexico->listaLexicos,lexico);
}

int tda_lexico_consultar(TDA_LEXICOS *tdaLexico, ITEM_LEXICO *lexico) {
	return LSO_ConsultarElemento(tdaLexico->listaLexicos,lexico);
}

int tda_lexico_modificar(TDA_LEXICOS *tdaLexico, ITEM_LEXICO *lexico) {
	return LSO_ModificarElemento(tdaLexico->listaLexicos,lexico);
}



int tda_lexico_printf(TDA_LEXICOS *tdaLexico) {
	ITEM_LEXICO itemLexico;

	if (!LSO_Vacia(*(tdaLexico->listaLexicos))) {
		LSO_Mover_Cte(tdaLexico->listaLexicos, LSO_Primero);

		do {
			//itemLexico = malloc(sizeof(ITEM_LEXICO));

			L_Elem_Cte(*(tdaLexico->listaLexicos), &itemLexico);

			printf("%s : %d\n", itemLexico.lexico,
					itemLexico.cantidadConsultasLexico);

			//printf("%s : %i",itemLexico.lexico,itemLexico.cantidadConsultasLexico);

		} while (LSO_Mover_Cte(tdaLexico->listaLexicos, LSO_Siguiente));

	}
}

int tda_lexico_read_file( TDA_LEXICOS *tdaLexico,FILE *fp) {

	char str1[100];

	//VA_create(array, sizeof(int)*10);

	while (fscanf(fp, "%s", str1) > 0) {
		//printf("%s\n", str1 );

		//VA_add(array,str1);

		char *ch;
		int indice = 0;

		ch = strtok(str1, ",");

		char *lexicoNombre;
		int lexicoCantidad;

		ITEM_LEXICO item;


		while (ch != NULL) {
			//printf("%s\n", ch);

			if (indice == 0) {
				strcpy(item.lexico, ch);
			}
			if (indice == 1) {
				item.cantidadConsultasLexico = atoi(ch);
			}

			ch = strtok(NULL, ",");
			indice++;
		}


		tda_lexico_alta(tdaLexico, &item);

	}

	return EXIT_SUCCESS;
}



int copy_files(char *file_name_source,char *file_name_target)
{
   char ch;
   FILE *source, *target;



   source = fopen(file_name_source, "r");

   if( source == NULL )
   {
      exit(EXIT_FAILURE);
   }


   target = fopen(file_name_target, "w");

   if( target == NULL )
   {
      fclose(source);
      exit(EXIT_FAILURE);
   }

   while( ( ch = fgetc(source) ) != EOF )
      fputc(ch, target);

   fclose(source);
   fclose(target);

   return 0;
}


int tda_lexico_write_file( TDA_LEXICOS *tdaLexico,FILE *fp) {
	ITEM_LEXICO itemLexico;

	if (!LSO_Vacia(*(tdaLexico->listaLexicos))) {
		LSO_Mover_Cte(tdaLexico->listaLexicos, LSO_Primero);

		do {
			//itemLexico = malloc(sizeof(ITEM_LEXICO));

			L_Elem_Cte(*(tdaLexico->listaLexicos), &itemLexico);

			char str[50];
			snprintf(str, 50, "%s,%d\n", itemLexico.lexico,itemLexico.cantidadConsultasLexico);

			fprintf(fp, str);

			//printf("%s : %i",itemLexico.lexico,itemLexico.cantidadConsultasLexico);

		} while (LSO_Mover_Cte(tdaLexico->listaLexicos, LSO_Siguiente));

	}


	return EXIT_SUCCESS;
}

